package com.attunlcok.unlock.service.impl;

import java.util.List;
import com.attunlcok.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.attunlcok.unlock.mapper.AttUserInfoMapper;
import com.attunlcok.unlock.domain.AttUserInfo;
import com.attunlcok.unlock.service.IAttUserInfoService;
import com.attunlcok.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;

/**
 * ATT用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class AttUserInfoServiceImpl implements IAttUserInfoService 
{
    @Autowired
    private AttUserInfoMapper attUserInfoMapper;

    /**
     * 查询ATT用户信息
     * 
     * @param id ATT用户信息主键
     * @return ATT用户信息
     */
    @Override
    public AttUserInfo selectAttUserInfoById(Long id)
    {
        return attUserInfoMapper.selectAttUserInfoById(id);
    }

    /**
     * 查询ATT用户信息列表
     * 
     * @param attUserInfo ATT用户信息
     * @return ATT用户信息
     */
    @Override
    public List<AttUserInfo> selectAttUserInfoList(AttUserInfo attUserInfo)
    {
        return attUserInfoMapper.selectAttUserInfoList(attUserInfo);
    }

    /**
     * 新增ATT用户信息
     * 
     * @param attUserInfo ATT用户信息
     * @return 结果
     */
    @Override
    public int insertAttUserInfo(AttUserInfo attUserInfo)
    {
        attUserInfo.setCreateTime(DateUtils.getNowDate());
        return attUserInfoMapper.insertAttUserInfo(attUserInfo);
    }

    /**
     * 修改ATT用户信息
     * 
     * @param attUserInfo ATT用户信息
     * @return 结果
     */
    @Override
    public int updateAttUserInfo(AttUserInfo attUserInfo)
    {
        attUserInfo.setUpdateTime(DateUtils.getNowDate());
        return attUserInfoMapper.updateAttUserInfo(attUserInfo);
    }

    /**
     * 批量删除ATT用户信息
     * 
     * @param ids 需要删除的ATT用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAttUserInfoByIds(String ids)
    {
        return attUserInfoMapper.deleteAttUserInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除ATT用户信息信息
     * 
     * @param id ATT用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAttUserInfoById(Long id)
    {
        return attUserInfoMapper.deleteAttUserInfoById(id);
    }

    /**
     * 导入用户数据
     * 
     * @param userData 用户数据字符串，每行一个用户
     * @param operName 操作人
     * @return 导入结果信息
     */
    @Override
    @Transactional
    public String importUserData(String userData, String operName)
    {
        if (StringUtils.isEmpty(userData))
        {
            return "导入数据不能为空！";
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        String[] lines = userData.split("\\r?\\n");
        Date now = DateUtils.getNowDate();

        for (String line : lines)
        {
            try
            {
                if (StringUtils.isEmpty(line))
                {
                    continue;
                }

                // 解析名、姓和手机号
                String[] parts = line.split("----");
                if (parts.length != 3)
                {
                    failureNum++;
                    failureMsg.append("<br/>第 " + (successNum + failureNum) + " 行数据格式不正确：" + line);
                    continue;
                }

                String firstName = parts[0].trim();
                String lastName = parts[1].trim();
                String phone = parts[2].trim();

                // 检查手机号格式
                if (!phone.matches("^\\d{10}$"))
                {
                    failureNum++;
                    failureMsg.append("<br/>第 " + (successNum + failureNum) + " 行手机号格式不正确：" + phone);
                    continue;
                }

                // 检查用户是否已存在
                AttUserInfo checkInfo = new AttUserInfo();
                checkInfo.setPhone(phone);
                List<AttUserInfo> existList = attUserInfoMapper.selectAttUserInfoList(checkInfo);
                if (!existList.isEmpty())
                {
                    failureNum++;
                    failureMsg.append("<br/>第 " + (successNum + failureNum) + " 行手机号已存在：" + phone);
                    continue;
                }

                // 创建新的用户信息
                AttUserInfo userInfo = new AttUserInfo();
                userInfo.setFirstName(firstName);
                userInfo.setLastName(lastName);
                userInfo.setPhone(phone);
                userInfo.setIsUsed("0");
                userInfo.setUsedCount(0L);
                userInfo.setStatus("0");
                userInfo.setCreateBy(operName);
                userInfo.setCreateTime(now);
                userInfo.setUpdateBy(operName);
                userInfo.setUpdateTime(now);

                attUserInfoMapper.insertAttUserInfo(userInfo);
                successNum++;
                successMsg.append("<br/>第 " + (successNum + failureNum) + " 行导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>第 " + (successNum + failureNum) + " 行导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}
